<?php
header("Content-Type: text/html;charset=utf-8");
define(SERVER_NAME, '云芯一号');
$title = sprintf("%s的监控界面", SERVER_NAME);
echo '<p style="font-size:24pt;color:red;text-align:center">'.$title.'<p>';
//定义常量
define(DB_HOST, '192.168.3.37');
define(DB_USER, 'apache');
define(DB_PASS, '这里写用户apache的密码');
define(DB_DATABASENAME, 'homeiot');
define(DB_TABLENAME, 'ctest');
define(DB_PORT, '3306');
//数据库表的列名
$dbcolarray = array('id', 'ishuman', 'data1', 'data2', 'time');
//mysql_connect
$conn=new mysqli(DB_HOST,DB_USER,DB_PASS,DB_DATABASENAME,DB_PORT);//获取数据库连接
mysqli_select_db(DB_DATABASENAME, $conn);

//读取表中纪录条数
$sql = sprintf("select count(*) from %s", DB_TABLENAME);
$result = mysqli_query($conn,$sql);
if ($result)
{
	$count = mysqli_fetch_row($result);
}
else
{
	die("query failed");
}
echo "表中有$count[0] 条记录<br />";

$sql = sprintf("select %s from %s order by id asc", implode(",",$dbcolarray), DB_TABLENAME);
mysqli_query($conn,$sql);
$result = $conn->query($sql); 
//$row=mysqli_fetch_assoc($result);

//表格
echo '<table id="Table" border=1 cellpadding=10 cellspacing=2 bordercolor=#ffaaoo>'; 
//表头
$thstr = "<th>" . implode("</th><th>", $dbcolarray) . "</th>";
echo $thstr;
//表中的内容
while ($row=mysqli_fetch_assoc($result))
{
	echo "<tr>";
	$tdstr = "";
	foreach ($dbcolarray as $td)
		$tdstr .= "<td>$row[$td]</td>";
	echo $tdstr;
	echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($conn);
?>

